#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  json_default.py
#       Author @  Huoty
#  Create date @  2016-07-27 15:25:48
#  Description @  
# *************************************************************

from __future__ import print_function

import json
import datetime
import functools

class Student(object):
    pass

class Teacher(object):
    pass

def serializer(obj):
    if isinstance(obj, (datetime.datetime, datetime.date)):
        return str(obj)
    elif isinstance(obj, (Student, Teacher)):
        return obj.__dict__
    else:
        raise TypeError("%s not serializable" % obj)

json_dumps = functools.partial(json.dumps, default=serializer)


# Script starts from here

if __name__ == "__main__":
    s = Student()
    s.name = "huoty"
    s.age = 25

    t = Teacher()
    t.name = "kalu"
    t.age = 23

    data = {
        "student": s,
        "teacher": t,
        "date": datetime.date.today()
    }

    print(json_dumps(data))

